
import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'
import { ADDUSERINFO } from '../store/mutations-type'
import { GetinfoApi } from "../request/api"
Vue.use(VueRouter)

const routes = [

  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login/index.vue')
  },
  {
    path: '/homepage',
    name: 'homepage',
    component: () => import(/* webpackChunkName: "about" */ '../views/Homepage/homepage.vue')
  }
  // {
  //   path: '/',
  //   name: 'Home',
  //   component: Home
  // },
  // {
  //   path: '/about',
  //   name: 'About',
  // route level code-splitting
  // this generates a separate chunk (about.[hash].js) for this route
  // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  // mode: 'hash',
  // base: process.env.BASE_URL,
  routes
})
//路由前置守卫
router.beforeEach((to, from, next) => {

  let token = localStorage.getItem('token');

  let newuserInfo = store.state.userInfo.menus;
  console.log(newuserInfo);
  //token有值且newuserIfon为空发送请求
  if (token && !newuserInfo) {
    GetinfoApi().then((res) => {
      if (res.code === 200) {
        console.log(res.data);
        store.commit(ADDUSERINFO, res.data);

      }
    });
  }
  next()
})


export default router
